<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <div></div>
        <script>
            let str = '()((()(())))(()())()';
            // let str = '()';

            var checkValidString = function (s) {
                let stack = [],
                    star = [];
                for (let i = 0; i < s.length; i++) {
                    // 一遍遍历， 对每一个s[i]，判断是入栈还是抵消
                    if (s[i] == '(') stack.push(i);
                    else if (s[i] == '*') star.push(i);
                    else {
                        // 是个 ")", 两个栈都不收，不仅不收，还要‘抵消’， 处理如下👇
                        if (stack.length) stack.pop();
                        else if (star.length) star.pop();
                        else return false; // 没得抵消了， 只能返回false
                    }
                }

                if (stack.length > star.length) return false; //对两个栈 处理
                while (stack.length && star.length) {
                    if (stack.pop() > star.pop()) return false; // **((, 类似这样，虽个数合适，但顺序（下标）不可
                }
                return true;
            };
            // console.log(checkValidString(str));

            // 作者：bzxspkcm
            // 链接：https://leetcode-cn.com/problems/valid-parenthesis-string/solution/js678you-xiao-de-gua-hao-zi-fu-chuan-by-0hzju/
            // 来源：力扣（LeetCode）
            // 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

            // function deal(ss) {
            //     let newSS = ss.replace(/\(\)/g, '').replace(/\)\(/g, ') (');
            //     console.log(newSS);
            //     let ssList = newSS.split(' ');
            //     console.log(ssList);
            //     for (let index = 0; index < ssList.length; index++) {}
            // }
            // deal(str);

            // 不带*
            console.log(deal1('()((()(())))(()())()'));
            console.log(deal1('()((())()'));
            console.log(deal1('()((()))()'));
            // let str = '()';
            function deal1(params) {
                console.log('----- deal1()', params);
                let ans = dealStr(params);
                console.log(ans);
                if (ans == '') {
                    return true;
                } else {
                    return false;
                }
            }

            function dealStr(so1) {
                console.log('----- dealStr()');
                console.log(so1);
                let sa2 = so1.replace(/\(\)/g, '');
                console.log(sa2);

                if (so1.length == sa2.length) {
                    console.log('return sa2 ', sa2);
                    return sa2;
                } else {
                    return dealStr(sa2);
                }
            }
        </script>
    </body>
</html>
